﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using DotNetOpenAuth.AspNet;
using Microsoft.Web.WebPages.OAuth;
using WebMatrix.WebData;
using mscook.mvc.Filters;
using mscook.mvc.Models;
using mscook.core.services;
using mscook.mvc.Core;
using mscook.core.repositories;
using mscook.core.domain;
using System.Security.Principal;
using mscook.mvc.core;
using mscook.core;
namespace mscook.mvc.Controllers
{
    public class MagazineController : BaseController
    {
        //
        // GET: /Magazine/
        private ILoginUserService iLoginUserService;
        private IMagazineService iMagazineService;
        private IArticleTemplateFactory articleTemplateFactory;
        private IPurchasedThingService iPurchasedThingService;
        private IMagazineLabelRepository iMagazineLabelRepository;
        private IResourceBonusSettingRepository iResourceBonusSettingRepository = UnityHelper.UnityToT<IResourceBonusSettingRepository>();
        public MagazineController(IActionBonusSettingService actionBonusSettingService, IResourceBonusSettingService resourceBonusSettingService, ILoginUserService loginUserService, IMagazineService iMagazineService, IArticleTemplateFactory articleTemplateFactory, IPurchasedThingService iPurchasedThingService, IMagazineLabelRepository iMagazineLabelRepository)
            : base(actionBonusSettingService, resourceBonusSettingService, loginUserService)
        {
            this.iLoginUserService = loginUserService;
            this.iMagazineService = iMagazineService;
            this.articleTemplateFactory = articleTemplateFactory;
            this.iPurchasedThingService = iPurchasedThingService;
            this.iMagazineLabelRepository = iMagazineLabelRepository;
        }

        public ActionResult Index(string[] resourceTypes, string num = "", string keyword = "")
        {
            MagazineModel magazineModel = new MagazineModel();
            magazineModel.LoginUser = CurrentUser;

            long total = 0;
            PageBreakHelper pageBreakHelper = new PageBreakHelper();
            pageBreakHelper.Init(num, "12");
            //magazineModel.MagazineList = iMagazineService.GetOrderByYearMonth(pageBreakHelper.Start, pageBreakHelper.PerValue, out total);
            if (resourceTypes != null)
            {
                IList<string> resoureTypeList = resourceTypes.Where(o => !string.IsNullOrEmpty(o)).ToList();
                //cookBookModel.CookBookList = iCookBookService.SelectPageList(pageBreakHelper.Start, pageBreakHelper.PerValue, out total);
                if (resoureTypeList.Count == 0)
                {
                    magazineModel.MagazineList = iMagazineService.SelectPageList(pageBreakHelper.Start, pageBreakHelper.PerValue, out total);
                }
                else
                {
                    magazineModel.MagazineList = iMagazineService.SelectLimitbyIds(GetRsourceIdsByTypes(resoureTypeList, PurchasedCategory.Magazine), pageBreakHelper.Start, pageBreakHelper.PerValue, out total);
                    magazineModel.SelectResourceTypes = resoureTypeList;
                }

            }
            else
            {

                magazineModel.MagazineList = iMagazineService.SelectPageList(pageBreakHelper.Start, pageBreakHelper.PerValue, out total);


            }


            ViewData["keyword"] = keyword;
            pageBreakHelper.SetViewDataKeys(ViewData, ViewBag, total);

            InitSourceType(magazineModel, PurchasedCategory.Magazine);
            InitNavBar();
            return View(magazineModel);
        }

        [ResourceBonusFilter]
        public ActionResult Inner(string id, int year = 0, int month = 0)
        {
            MagazineInnerModel magazineInnerModel = new MagazineInnerModel();
            magazineInnerModel.Magazine = iMagazineService.Get(id);
            magazineInnerModel.ArticlePublishDetails = articleTemplateFactory.SelectArticlePublishDetails(year, month).Where(o => o.isPublished).ToList();

            IList<string> purchasedArticleIds = iPurchasedThingService.GetBy(CurrentUser, PurchasedCategory.Article).Select(o => o.RelatedId).ToList();
            magazineInnerModel.PurchasedArticle = magazineInnerModel.ArticlePublishDetails.Where(o => purchasedArticleIds.Contains(o.ArticleKey.Id()) && o.isPublished).ToList();

            InitNavBar();
            return View(magazineInnerModel);
        }
        [ResourceBonusFilter]
        public ActionResult ReadArticle(int year, int month, string id)
        {
            ReadArticleModel readArticleModel = new ReadArticleModel() { LoginUser=CurrentUser};
            long total;
            IList<ArticlePublishDetail> articlePublishDetails = articleTemplateFactory.SelectArticlePublishDetails(year, month, 0, 100, out total);
            Magazine magazine = iMagazineService.GetByYearMonth(year.ToString(), month.ToString());
            if (!string.IsNullOrEmpty(id) && magazine.Id.Equals(id))
            {
                magazine.ReadTimes += 1;
                iMagazineService.SaveOrUpdate(magazine);

                readArticleModel.Magazine = magazine;
                readArticleModel.ArticlePublishDetails = articlePublishDetails;

                MagazineLabel magazineLabel = iMagazineLabelRepository.GetByMagazineAndUser(magazine.Id, CurrentUser == null ? "" : CurrentUser.Id);
                 readArticleModel.PageNumber = magazineLabel == null ? 0 : magazineLabel.PageNumber;

                //ViewData["MagazineImage"] = magazine == null ? "" : magazine.ImageName;
                //ViewData["MagazineId"] = magazine == null ? "" : magazine.Id;
                 readArticleModel.RelativeLinks = GetRelativeLinks(articlePublishDetails.Select(o=>o.ArticleKey.Id()).ToList());

                 readArticleModel.ResourceBonusSettings = GetBonusByResourceIds(readArticleModel.RelativeLinks.Select(o=>o.LinkId).ToList());

                return View(readArticleModel);
            }
            return Redirect("/");



        }

        public void SaveMagazineLabel(int pageNumber, string magazineId)
        {
            if (CurrentUser != null)
            {

                MagazineLabel magazineLabel = iMagazineLabelRepository.GetByMagazineAndUser(magazineId,CurrentUser.Id);
                if (magazineLabel == null)
                {

                    magazineLabel=new MagazineLabel() { PageNumber = pageNumber, MagazineId = magazineId };
                    magazineLabel.LoginUserId = CurrentUser.Id;
                    
                }
                else
                {
                    magazineLabel.PageNumber = pageNumber;
                }

                iMagazineLabelRepository.SaveOrUpdate(magazineLabel);
            }
        }

    }
}
